package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.model.TeacherApproval;
import com.example.demo.service.TeacherApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 教师权限申请审批控制器
 */
@RestController
@RequestMapping("/teacher-approvals")
public class TeacherApprovalController {

    @Autowired
    private TeacherApprovalService teacherApprovalService;

    /**
     * 创建权限申请
     */
    @PostMapping
    public Result<TeacherApproval> createApproval(@RequestBody Map<String, Object> request) {
        Integer userId = (Integer) request.get("userId");
        String requestedRole = (String) request.get("requestedRole");
        String approvalComment = (String) request.get("approvalComment");
        
        return Result.success(teacherApprovalService.createApproval(userId, requestedRole, approvalComment));
    }
    
    /**
     * 审批权限申请
     */
    @PostMapping("/{id}/approve")
    public Result<TeacherApproval> approveRequest(
            @PathVariable("id") Integer approvalId,
            @RequestBody Map<String, Object> request) {
        Integer approverId = (Integer) request.get("approverId");
        Boolean isApproved = (Boolean) request.get("isApproved");
        String comment = (String) request.get("comment");
        
        return Result.success(teacherApprovalService.approveRequest(approvalId, approverId, isApproved, comment));
    }
    
    /**
     * 获取申请详情
     */
    @GetMapping("/{id}")
    public Result<TeacherApproval> findById(@PathVariable("id") Integer approvalId) {
        return Result.success(teacherApprovalService.findById(approvalId));
    }
    
    /**
     * 获取用户的申请列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<TeacherApproval>> findByUserId(@PathVariable("userId") Integer userId) {
        return Result.success(teacherApprovalService.findByUserId(userId));
    }
    
    /**
     * 获取指定状态的申请列表
     */
    @GetMapping("/status/{status}")
    public Result<List<TeacherApproval>> findByStatus(@PathVariable("status") String status) {
        return Result.success(teacherApprovalService.findByStatus(status));
    }
    
    /**
     * 获取所有申请
     */
    @GetMapping
    public Result<List<TeacherApproval>> findAll() {
        return Result.success(teacherApprovalService.findAll());
    }
    
    /**
     * 检查用户是否有权限审批
     */
    @GetMapping("/can-approve")
    public Result<Boolean> canApprove(
            @RequestParam Integer approverId,
            @RequestParam String requestedRole) {
        return Result.success(teacherApprovalService.canApprove(approverId, requestedRole));
    }
} 